package com.healthcloud.service.accompany.impl;

import cn.hutool.core.collection.CollUtil;
import com.healthcloud.dao.accompany.AccompanyBlDao;
import com.healthcloud.entity.HlDoctorEntity;
import com.healthcloud.entity.accompany.AccompanyBlEntity;
import com.healthcloud.entity.accompany.AccompanyBlVo;
import com.healthcloud.enums.AccompanyBlHasMedicalFlagEnum;
import com.healthcloud.service.HlDoctorService;
import com.healthcloud.service.accompany.AccompanyBlService;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("accompanyBlService")
@Transactional
public class AccompanyBlServiceImpl implements AccompanyBlService {
    @Autowired
    private AccompanyBlDao accompanyBlDao;

    @Autowired
    private HlDoctorService hlDoctorService;

    @Override
    public AccompanyBlVo queryObjectVo(Long id) {
        return accompanyBlDao.queryObjectVo(id);
    }

    @Override
    public List<AccompanyBlVo> queryListVo(AccompanyBlVo entity, PageParam pageParam) {
        return accompanyBlDao.queryListVo(entity, pageParam);
    }

    @Override
    public int queryTotalVo(AccompanyBlVo entity) {
        return accompanyBlDao.queryTotalVo(entity);
    }

    @Override
    public void save(AccompanyBlEntity accompanyBl) {
        accompanyBlDao.save(accompanyBl);
    }

    @Override
    public void update(AccompanyBlEntity accompanyBl) {
        accompanyBlDao.update(accompanyBl);
    }

    @Override
    public void delete(Integer id) {
        accompanyBlDao.delete(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        accompanyBlDao.deleteBatch(ids);
    }

    @Override
    public void saveOrUpdate(AccompanyBlEntity entity) {
        if (entity.getId() == null) {
            saveByDoctor(entity);
        } else {
            update(entity);
        }
    }

    private void saveByDoctor(AccompanyBlEntity accompanyBl) {
        Long userId = ShiroUtils.getUserId();
        HlDoctorEntity doctor = hlDoctorService.queryByUserId(userId);
        if (doctor != null) {
            accompanyBl.setDoctorId(doctor.getId());
        }
        accompanyBl.setHasMedical(AccompanyBlHasMedicalFlagEnum.HAS_MEDICAL.getCode()); // 有药物信息
        save(accompanyBl);
    }

    @Override
    public List queryRemindList(AccompanyBlVo entity, PageParam pageParam) {
        return accompanyBlDao.queryRemindList(entity, pageParam);
    }

    @Override
    public Integer queryRemindTotal(AccompanyBlVo entity) {
        return accompanyBlDao.queryRemindTotal(entity);
    }

    @Override
    public void deleteByIds(List<Long> accompanyBLIds) {
        if (CollUtil.isNotEmpty(accompanyBLIds)) {
            accompanyBlDao.deleteByIds(accompanyBLIds);
        }
    }

    /**
     * 根据身份证号，查询上一次有药物骨松随访的 id
     * @param idCard
     * @return
     */
    @Override
    public Integer getLastBlIdWithMedical(String idCard) {
        return accompanyBlDao.getLastBlIdWithMedical(idCard, AccompanyBlHasMedicalFlagEnum.HAS_MEDICAL.getCode());
    }

}